$(document).ready(function(){

    //上传车标
    $("#car-logo").click(function(){
        $("#car_thelist1").html(null);
        $(".Validform_checktip").removeClass("Validform_wrong").removeClass("Validform_right").text("");
        $(".demoform2 label").removeClass("select");
        $(".img-name").html(null);  //初始化放置 img返回的名字为空
        $(".project2").attr("value","");$("#car_logo_input").val(null);$("#thelist4").html(null);
        upTp("car_webuploder4", '${ctxStatic }/webupload/Uploader.swf','http://up-z1.qiniu.com/', '#car_filePicker1', "#car_thelist1", "#car_btn1","car_logo_img/",1);
    });
    //更改车标
    $("#car_icon_changge").click(function(){
        $(".img-name").html(null); //初始化放置 img返回的名字为空
        upTp("car_webuploder5", '../webuploder/Uploader.swf','http://up-z2.qiniu.com/', '#filePicker-car-change', "#thelist-car-change", "#ctlBtn-car-change","goods_category_img/",1);//商品上传
    })
    //显示 删除键
    $("#example21").ajaxComplete(function(){
        $(".yyy").addClass("car-hide");
        $(".car-1").hover(function(){
            $(this).find("i").removeClass("car-hide").addClass("car-show");
        },function(){
            $(this).find("i").removeClass("car-show").addClass("car-hide");
        });

        //双击修改车型
        MibiChange("#example21",".car-1",".car-name");
//        $(".car-1").dblclick(function(){
//            $(this).find("i").removeClass("car-show").addClass("car-hide");
//            var new_modal={};
//            new_modal.modal_id=$(this).find("i").next().text();        //获取点击模型的 ID
//            new_modal.icon_id=$(this).parent().prev().prev().find("span").text(); //获取车辆图标的 ID
//            var all2 = $(this);
//            var change2 = all2.find(".car-name");
//            var changeText2 = change2.text();
//            var input2 = $('<input type="text" class="text-center car-name-1" id="cid2" value="' + changeText2 + '"/>');
//            change2.html(input2);
//            input2.click(function () {
//                return false;
//            });
//            input2.trigger("focus");
//            input2.blur(function () {
//                if (input2.val() == "") {
//                    swal("输入不能为空！！");
//                    input2.val(changeText2)
//                    return
//                }
//                var newText2 = input2.val();
//                //   console.log(newText2);
////判断文本有没有修改
//                change2.html(newText2);
//                console.log(newText2);
//                new_modal.name=newText2;
//                TJ_ajax("test3.json",null,new_modal);
//            });
//        });

        //点击删除键 删除车标模型
        $(".yyy").click(function(){
            var jj={};
            var _this=$(this);
            jj.model_id=$(this).next().text();
            jj.icon_id=$(this).parent().parent().prev().prev().find("span").text();
            swal({   title: "确定要删除吗?",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "rgb(140, 212, 245)",
                    confirmButtonText: "删除",
                    cancelButtonText: "再想想！",
                    closeOnConfirm: false },
                function(){
                    _this.parent().remove();
                    console.log(jj)
                    //TJ_ajax("test3.json",jj);
                    swal("", "删除成功！！", "success");
                });

        })
    });

    //多选框样式 js
    $(".project2 label").click(function(){
        var ss=$(this).attr("value");
        $(this).attr("class","select").siblings().attr("class","unselect");
        $(this).parent().attr("value",ss);
    });
    //车标提交
    var select=1;
    $(".car_logo").click(function(){
        var vlue=$(".project2").attr("value");
        var img_name=$(this).parent().parent().next().text();
        var name=$("#car_logo_input").val();
        if(vlue==""||vlue==undefined||img_name==""||img_name==undefined||name==""||$("#car_logo_input").hasClass("Validform_error")){
            swal("你有任务没完成！");
            return
        };
        var ddate={
            brand_type:vlue,
            vehicle_brand_img:img_name,
            vehicle_brand:name
        };
        $.ajax({
            type:"POST",
            url:address1+"/admin/Vehicle/uploadBrand",
            dataType:"json",
            data:ddate,
            success:function(data){

                $("#thelist4").html(null);
                $("#car_icon .img-name").html(null);
                $("#modal-car-icon").modal("hide");
                var table = $('#example21').DataTable();
                table.ajax.reload();
                swal("OK")

            },
            error:function(){
                swal("请求失败");
            }
        })

    });
    $("#car_pinpai_select").change(function(){
        var options=$("#car_pinpai_select option:selected"); //获取选中的项
        select=options.val();
        console.log(select)
    });

    //型号点击时候获取已有的车标
    $("#car_model_click").click(function(){
        $("#car_pinpai_select").html(null);
        $.ajax({
            type:"POST",
            url:address1+"/admin/Vehicle/getVehicleBrand",
            dataType:"json",
            data:"",
            success:function(data){
                var car=data.data;
                var postiioon;
                for(var i=0;i<car.length;i++){

                    if(car[i].vehicle_brand_id==select){
                        postiioon+="<option  selected = 'selected'  value='"+car[i].vehicle_brand_id+"'>"+car[i].vehicle_brand+"</option>"
                    }else{
                        postiioon+="<option value='"+car[i].vehicle_brand_id+"'>"+car[i].vehicle_brand+"</option>";
                    }
                }
                $("#car_pinpai_select").append(postiioon);
            },
            error:function(){
                swal("请求失败");
            }
        });
    });

    //型号提交按钮
    $(".car_XH").click(function(){
        var options=$("#car_pinpai_select option:selected"); //获取选中的项
        var car_type=$("#car_type").val();
        select=options.val();
        if(car_type == ""){
            swal("请输入车辆类型！！")
            return
        }
        var car={
            vehicle_brand_id:options.val(),
            vehicle_model:car_type,
            vehicle_model_img:""
        }
        $.ajax({
            type:"POST",
            url:address1+"/admin/Vehicle/uploadModel",
            dataType:"json",
            data:car,
            success:function(data){
                $("#modal-car-type").modal("hide");
                var table = $('#example21').DataTable();
                table.ajax.reload();

                swal("OK")
            },
            error:function(){
                swal("请求失败");
            }
        });
    });

    // 在点击 车标管理 的时候 加载所有的车辆信息
    $(".car").click(function () {
           var cc = {
           	jai : true
           };
        var car_show = [
            {  data: function (obj) {
                return "<a href='Javascript:;'><img width='80'  src='" +obj.vehicle_brand_img + "' alt='' data-toggle='modal' id='car_icon_changge' data-target='#change-car-icon'>" +
                    "<span style='display: none'>"+obj.vehicle_brand_id+"</span></a>";
            }
            },
            {data: "vehicle_brand"},
            {
                data: function (obj) {
                    var car_modal=obj.vehicle_model;
                    $div="";
                    $.each(car_modal,function(i){
                        $div+= "<div class='btn car-1'><span class='car-name' style='display: inline-block'>"+car_modal[i].vehicle_model+"</span>" +
                            "<i class='yyy'><span class='icon-remove'></span></i>" +
                            "<span class='car_model_id' style='display: none'>"+car_modal[i].vehicle_model_id+"</span></div>";
                    })
                    return $div
                }
            }
        ]
        Data_table("#example21", address1+"/admin/Vehicle/vehicleBrandMain",car_show,false,cc,5);
    });
});